package com.controller;

import com.common.Result;
import com.common.ResultEnum;
import com.common.log.OperationLog;
import com.common.log.OperationType;
import com.entity.Email;
import com.entity.EmailContacts;
import com.service.EmailService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @ClassName: EmailController
 * @Author: ARongzhe
 * @Date: 2022/04/27 19:40:38
 * @Description: TODO
 */
@RestController
@RequestMapping("email")
public class EmailController {
    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private EmailService emailService;

    @ApiOperation("发送邮件")
    @RequestMapping("send1")
    @OperationLog(operationType = OperationType.INSERT, describe = "发送简单邮件", entity = Email.class, primaryKey = "id")
    public Result send(@RequestBody Email email) {
        //创建信息体
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        if (email.getMailTo().length() == 0 || email.getMailSubject().length() == 0 || email.getMailText().length() == 0) {
            return Result.fail(ResultEnum.ERROR.getCode(), "发送失败，请重新发送");
        }
        //收件人
        simpleMailMessage.setTo(email.getMailTo());
        //发件人
        simpleMailMessage.setFrom("1127037972@qq.com");

        //主题
        simpleMailMessage.setSubject(email.getMailSubject());
        //email内容
        simpleMailMessage.setText(email.getMailText());
        if (email.getMailCc().length() > 0) {
            //抄送
            simpleMailMessage.setCc(email.getMailCc());
        }

        //发送邮件
        //javaMailSender.send(simpleMailMessage);
        email.setMailFrom("1127037972@qq.com");
        emailService.addEmail(email);
        return Result.success("200", "发送成功");
    }

    @ApiOperation("发送邮件")
    @RequestMapping("send")
    @OperationLog(operationType = OperationType.INSERT, describe = "发送复杂邮件", entity = Email.class, primaryKey = "id")
    public Result sendHard(@RequestBody Email email) throws MessagingException {
        //发送复杂邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        //校验参数
        if (email.getMailTo().length() == 0 || email.getMailSubject().length() == 0 || email.getMailText().length() == 0) {
            return Result.fail(ResultEnum.ERROR.getCode(), "发送失败，请重新发送");
        }
        //发件人
        mimeMessageHelper.setFrom("1127037972@qq.com");
        //收件人
        mimeMessageHelper.setTo(email.getMailTo());
        //主题
        mimeMessageHelper.setSubject(email.getMailSubject());
        //邮件内容
        mimeMessageHelper.setText(email.getMailText());
        //校验是否存在抄送人
        if (email.getMailCc().length() > 0) {
            //抄送
            mimeMessageHelper.setCc(email.getMailCc());
        }
        System.out.println("email.getMailAddress() = " + email.getMailAddress());
        File file = new File(email.getMailAddress());

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("G:\\GraduaionProject\\PersonnelSystem\\v-personnel-server\\target\\classes\\static\\").append(email.getMailAddress());
        //校验是否存在附件地址
        if (email.getMailAddress().length() > 0) {
            //创建系统文件资源
            File trueFile = new File(stringBuilder.toString());
            FileSystemResource fileSystemResource = new FileSystemResource(trueFile);
            mimeMessageHelper.addAttachment(file.getName(), fileSystemResource);
            email.setMailAddress("http://127.0.0.1:8083/emailFile/uploadFile/" + file.getName());
            email.setAccessoryAddressStatu("YES");
        }else{
            email.setAccessoryAddressStatu("NO");
        }
        //发送邮件
        javaMailSender.send(mimeMessage);
        //添加发件人信息
        email.setMailFrom("1127037972@qq.com");

        //添加到数据库中
        emailService.addEmail(email);
        return Result.success("200", "发送成功");
    }

    @ApiOperation("查看所有已发送邮件")
    @RequestMapping("findAll")
    public Result findAll() {
        return Result.success("200",  emailService.getAllEmail());
    }

    @ApiOperation("通过收件人查找邮件")
    @RequestMapping("findByMailTo/{mailTo}")
    public Result findByMailTo(@PathVariable String mailTo){
    	return Result.success("200",  emailService.findBymailTo(mailTo));
    }

    @ApiOperation("通过备注查询企业邮箱")
    @RequestMapping("findByRemarkName/{remarkName}")
    public Result findContactsByRemarkName(@PathVariable String remarkName){
        return Result.success("200",  emailService.findContactsByRemarkName(remarkName));
    }

    @ApiOperation("查询所有的企业邮箱信息")
    @RequestMapping("findAllEmailContacts")
    public Result findAllContacts(){
        return Result.success("200",  emailService.findAllEmailContacts());
    }

    @ApiOperation("添加企业邮箱信息")
    @RequestMapping("addEmailContacts")
    public Result addEmailContacts(@RequestBody EmailContacts emailContacts){
        boolean addEmailContactsStatus = emailService.addEmailContacts(emailContacts);
        if(addEmailContactsStatus){
            return Result.success("200", "添加成功");
        }else {
            return Result.fail(ResultEnum.ERROR.getCode(), "添加失败");
        }
    }

    @ApiOperation("删除企业邮箱信息")
    @RequestMapping("deleteEmailContacts/{id}")
    public Result deleteEmailContacts(@PathVariable String id){
        boolean deleteEmailContactsStatus = emailService.deleteEmailContacts(id);
        if (deleteEmailContactsStatus==true){
            return Result.success("200",  "删除成功");
        }else{
            return Result.fail(ResultEnum.ERROR.getCode(),  "已存在该邮箱！！！");
        }
    }

    @ApiOperation("修改企业邮箱信息")
    @RequestMapping("updateEmailContacts")
    public Result updateEmailContacts(@RequestBody EmailContacts emailContacts){
        boolean updateEmailContactsStatus = emailService.updateEmailContacts(emailContacts);
        if (updateEmailContactsStatus==true){
            return Result.success("200",  "修改成功");
        }else {
            return Result.fail(ResultEnum.ERROR.getCode(),  "修改失败");
        }
    }

    @ApiOperation("删除已发送的邮件")
    @RequestMapping("deleteEmail/{id}")
    public Result deleteEmail(@PathVariable String id){
        boolean deleteEmailStatus = emailService.deleteEmail(id);
        if (deleteEmailStatus==true){
            return Result.success("200",  "删除成功");
        }else{
            return Result.fail(ResultEnum.ERROR.getCode(),  "删除失败");
        }
    }

    //
}
